<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章分类</span>
        <el-button @click="addCateBtnFn" style="float: right; " type="primary" size="mini">添加分类</el-button>
      </div>
      <el-table style="width: 100%" :data="cateList" border stripe>
        <el-table-column label="序号" type="index" width="100"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="updateFn(scope.row)">修改</el-button>
            <el-button type="danger" @click="removeFn(scope.row.id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog :title="isEdit ? '编辑文章分类' : '添加文章分类'" :visible.sync="addVisible" width="35%" @close="onAddClosedFn">
        <el-form ref="ruleForm" :model="addForm" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="文章分类" prop="cate_name">
            <el-input v-model="addForm.cate_name"></el-input>
          </el-form-item>
          <el-form-item label="文章别名" prop="cate_alias">
            <el-input v-model="addForm.cate_alias"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini" @click="cancelFn">取 消</el-button>
          <el-button size="mini" type="primary" @click="addFn">添加</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { getArtCateListAPI, addArtCateAPI, updateArtCateAPI, delArtCateAPI } from '@/api'
export default {
  name: 'ArtCile',
  data () {
    return {
      // 文章的分类列表
      cateList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editId: '', // 要修改的文章id
      isEdit: false, // 保存编辑的状态  false新增 true修改
      deleteId: '',
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.getInfo()
  },
  methods: {
    async getInfo () {
      const res = await getArtCateListAPI()
      this.cateList = res.data.data
    },
    // 添加分类按钮
    addCateBtnFn () {
      this.addVisible = true
      this.editId = ''
      this.isEdit = false
    },
    // 对话框的取消按钮
    cancelFn () {
      this.addVisible = false
    },
    // 对话框的提交按钮
    async addFn () {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            const { data: res } = await updateArtCateAPI({ id: this.editId, ...this.addForm })
            if (res.code !== 0) return this.$message.error('更新分类失败')
            this.$message.success('更新分类成功')
          } else {
            const { data: res } = await addArtCateAPI(this.addForm)
            if (res.code !== 0) return this.$message.error('添加分类失败')
            this.$message.success('添加分类成功')
          }
          this.getInfo()
          this.addVisible = false
        } else {
          return false
        }
      })
    },
    /* 对话框关闭的回调函数 */
    onAddClosedFn () {
      this.$refs.ruleForm.resetFields()
    },
    // 表格的修改按钮
    updateFn (obj) {
      this.addVisible = true
      this.editId = obj.id
      this.isEdit = true
      this.$nextTick(() => {
        this.addForm.cate_name = obj.cate_name
        this.addForm.cate_alias = obj.cate_alias
      })
    },
    // 表格的删除按钮
    removeFn (id) {
      this.$confirm('你确定删除吗? 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await delArtCateAPI(id)
        if (res.code !== 0) {
          return this.$message.error('删除失败')
        }
        this.$message.success('删除成功')
        this.getInfo()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>
<style lang="less" scoped>
</style>
